package com.skyz.base.http;

import com.skyz.base.manager.LoadingManager;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.SingleObserver;
import io.reactivex.rxjava3.disposables.Disposable;

public abstract class HttpRequestCallback<T> implements SingleObserver<T> {

    //是否需要展示加载框
    private final boolean mShowLoading;

    protected HttpRequestCallback(boolean showLoading) {
        this.mShowLoading = showLoading;
    }

    @Override
    public void onSubscribe(@NonNull Disposable d) {
        if (mShowLoading) {
            LoadingManager.getInstance().requestShowDialog();
        }
    }

    @Override
    public void onSuccess(@NonNull T t) {
        if (mShowLoading) {
            LoadingManager.getInstance().requestDismissDialog();
        }
        onHttpRequestSuccess(t);
    }

    @Override
    public void onError(@NonNull Throwable e) {
        if (mShowLoading) {
            LoadingManager.getInstance().requestDismissDialog();
        }
        onHttpRequestFail(e);
    }

    protected abstract void onHttpRequestSuccess(T t);

    protected abstract void onHttpRequestFail(Throwable e);
}
